package com.huazhi.wzsnw.util;

import com.huazhi.wzsnw.model.po.MenuView;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    public static List<MenuView> toTree(List<MenuView> treeList, String parentTid) {
        List<MenuView> retList = new ArrayList<>();
        for (MenuView parent : treeList) {
            if (parentTid.equals(parent.getParentTid())) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static MenuView findChildren(MenuView parent, List<MenuView> treeList) {
        for (MenuView child : treeList) {
            if (parent.getTid().equals(child.getParentTid())) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<MenuView>());
                }
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }
}
